local PartnerHelper = {}


-- 缓存一些配置
function PartnerHelper.cacheConfig()
    PartnerHelper.isCache = true

    -- 随从 品质划分
    local queryConfigs = GD:queryPartnetQualityConfig()
    PartnerHelper.queryConfigs = queryConfigs
    
    -- 随从羁绊 点品质 对应评分
    local relationScoreConfigs = {}
    for k ,v in ipairs(queryConfigs) do
        relationScoreConfigs[v.id] = v.relationScore
    end

    PartnerHelper.relationScoreConfigs = relationScoreConfigs
end

-- 根据战力 获取随从品质 配置
function PartnerHelper.getPartnerQualityConfigByFightNum(fightNum)
    if not PartnerHelper.isCache then
        PartnerHelper.cacheConfig()
    end
    local configs = PartnerHelper.queryConfigs
    for k ,v in ipairs(configs) do
        if v.powerMin <= fightNum and fightNum < v.powerMax then
            return v
        end
    end
    return configs[#configs]
end

-- 根据战力 获取随从品质
function PartnerHelper.getPartnerQualityByFightNum(fightNum)
    local config = PartnerHelper.getPartnerQualityConfigByFightNum(fightNum)
    return config.id
end

function PartnerHelper.getRelationScoreConfig()
    if not PartnerHelper.isCache then
        PartnerHelper.cacheConfig()
    end
    return PartnerHelper.relationScoreConfigs
end

-- 根据品质 获取羁绊点评分
function PartnerHelper.getPartnerRelationScoreByQuality(quality)
    if not PartnerHelper.isCache then
        PartnerHelper.cacheConfig()
    end
    return PartnerHelper.relationScoreConfigs[quality]
end

function PartnerHelper.getPartnerRelationColorByQuality(quality)
    if not PartnerHelper.isCache then
        PartnerHelper.cacheConfig()
    end
    local colorStr = string.split(PartnerHelper.queryConfigs[quality].qualityStringColor,"#")
    local color = cc.c4b(checknumber(colorStr[1]),checknumber(colorStr[2]),checknumber(colorStr[3]),checknumber(colorStr[4]))
    return color
end

function PartnerHelper.getPartnerRelationColorStringByQuality(quality)
    if not PartnerHelper.isCache then
        PartnerHelper.cacheConfig()
    end
    return L(PartnerHelper.queryConfigs[quality].qualityString)
end

-- 获取羁绊 品级
function PartnerHelper.getPartnerRelationLevelByTotalScore(score)
    local rows = GD:getPartnerRelationScore()
    local configs = {}
    for k, v in pairs(rows) do
        local conf = {
            name = v.scoreName,
            score = tonumber(v.score),
            buster = tonumber(v.buster)
        }
        table.insert(configs, conf)
    end

    table.sort(configs, function(a, b) 
        return a.score > b.score
    end)

    for k ,v in pairs(configs) do
        if score >= v.score then
            return v, configs[k-1]
        end
    end
    return configs[#configs]
end


-- 初始化所有 随从
function PartnerHelper.initPartners()
    local rows = GD:getPartners()
    local Partner = require("app.public.dao.Partner")
    local PartnerEquip = require("app.public.dao.PartnerEquip")
    local t = {}
    for k, v in ipairs(rows) do
        local p = Partner.new()
        p:initWithConf(v)
        p:initSkill()
        t[tonumber(v.ID)] = p
    end
    
    local equipRows = GD:getPartnerEquips()
    for k, v in ipairs(equipRows) do
        local e = PartnerEquip.new()
        e:initByConfig(v)
        local p = t[tonumber(v.belongTo)]
        if p then
            p:addEquip(e)
            if p:isSpReward() then
                e:setDisableStrength(true)
            end
        end
    end
    return t
end


function PartnerHelper.createRelationData()
    local d = {
        {name = L("RelationUpgradeTab1"), icon = "partner_jiban_icon_wuqi.png",bg = "partner_img_wuqi.png", propId = 91, partners = {}, relations = {}},
        {name = L("RelationUpgradeTab2"), icon = "partner_jiban_icon_fa.png", bg = "partner_img_fa.png",propId = 93, partners = {}, relations = {}},
        {name = L("RelationUpgradeTab3"), icon = "partner_jiban_icon_yuan.png", bg = "partner_img_yuan.png",propId = 92, partners = {}, relations = {}},
        {name = L("RelationUpgradeTab4"), icon = "partner_jiban_icon_ziran.png", bg = "partner_img_ziran.png",propId = 94, partners = {}, relations = {}},
    }
  

    local ps = UD.partnersData:getPartners()
    for k ,v in pairs(ps) do
        local relationNum = v:getRelationNum()
        local index = math.floor(relationNum / 100)
        local pos = relationNum % 100
  
        if d[index] then
            d[index].partners[pos] = v
        end
    end
    local PartnerRelation = require("app.public.dao.PartnerRelation")
    local rows = GD:getPartnerRelation()
    for k ,v in pairs(rows) do
        local index = tonumber(v.tabIndex)
        if index and d[index] then
            local relationData = PartnerRelation.new()
            relationData:initWithConf(v)
            d[index].relations[relationData:getPos()] = relationData
        end
    end
    return d
end


return PartnerHelper